﻿package com.tangchao.client.sdh.control
{
	import com.smartfoxserver.v2.entities.SFSUser;
	import com.tangchao.client.entities.User;
	import com.tangchao.client.sdh.definition.DFCardColor;
	import com.tangchao.client.sdh.definition.DFState;
	import com.tangchao.client.sdh.definition.DFSuoFangDaXiao;
	import com.tangchao.client.sdh.event.DaoGenFangEvent;
	import com.tangchao.client.sdh.event.JiaoFenEvent;
	import com.tangchao.client.sdh.event.LoginEvent;
	import com.tangchao.client.sdh.event.PlayEvent;
	import com.tangchao.client.sdh.event.ReadyEvent;
	import com.tangchao.client.sdh.event.SelectColorEvent;
	import com.tangchao.client.sdh.sdhinterface.In;
	import com.tangchao.client.sdh.sdhinterface.Out;
	import com.tangchao.client.sdh.view.JieSuan;
	import com.tangchao.client.sdh.view.RoleInfo;
	import com.tangchao.client.sdh.view.SanDaHaPanel;
	import com.tangchao.client.sdh.view.SuoFangDaXiao;
	import com.tangchao.client.sdh.vo.JieSuanVo;
	import com.tangchao.client.sdh.vo.RoleInfoVo;
	import com.tangchao.client.sdh.vo.RoleJieSuanVo;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.text.TextField;

	public class SanDaHaControl extends MovieClip
	{
		private var _login:LogIn;
		private var _sandahaPanel:SanDaHaPanel;
		private var _nowState:int=DFState.LOGIN;
		private var _tfStatInfo:TextField;
		
		static private var _instance:SanDaHaControl;
		
		public function SanDaHaControl()
		{
			init();
			addEventListener(ReadyEvent.READY,onReady);
			addEventListener(LoginEvent.LOGIN,onLoginGame);
			addEventListener(JiaoFenEvent.JIAOFEN,onJiaoFenEvent);
			addEventListener(SelectColorEvent.SELECT_COLOR,onSelectColor);
			addEventListener(PlayEvent.OUT_CARD,onOutCard);
			//addEventListener(PlayEvent.Prompt,onPrompt);
			addEventListener(PlayEvent.GAIPAI_CARD,onGaiPaiCard);
			addEventListener(DaoGenFangEvent.DAO_GEN_FANG,onDaoGenFang);
			
			_instance = this;
		}
		
		static public function getInstance():SanDaHaControl
		{
			return _instance;
		}
		
		public function setMsg(msg:String):void
		{
			if(!msg)
			{
				msg = "";
			}
			_tfStatInfo.text = msg;
		}
		
		/*
		*主动接口，通知前台逻辑用户点击倒或跟或反，e.index==1则为倒，==2则为跟，==3则为反
		*/
		private function onDaoGenFang(e:DaoGenFangEvent)
		{
			switch(e.index)
			{
				case 1:
					Out.getInstance().onDao();
					break;
				case 2:
					Out.getInstance().onGen();
					break;
				case 3:
					Out.getInstance().onFan();
					break;
				case 4:
					Out.getInstance().onTuoGuan(false);
					break;
				case 5:
					Out.getInstance().onTuoGuan(true);
					break;
				default:
					break;
			}
		}
		
		/*
		*主动接口，通知前台逻辑用户盖底牌，e.card为一个底牌数组，每个均为一个代表牌的index
		*/
		private function onGaiPaiCard(e:PlayEvent)
		{
			var s:String=e.card.toString();
			Out.getInstance().onPutBottomCard(e.card);
		}
		
		public function onReady(e:ReadyEvent)
		{
			Out.getInstance().onReady();
		}
		
		
		/*
		*主动接口，通知前台逻辑用户出牌，e.card为一个底牌数组，每个均为一个代表牌的index
		*/
		private function onOutCard(e:PlayEvent)
		{
			Out.getInstance().onPlayCars(e.card);
		}
		
		/*
		*主动接口，通知前台逻辑用户叫分，有2个参数：isJiao是否叫分，value为叫的分值
		*/
		private function onJiaoFenEvent(e:JiaoFenEvent)
		{
			trace(this, e.isJiao, e.value);
			_sandahaPanel.hideJiaoFen();
			_sandahaPanel.hideClock();
			Out.getInstance().onBet(e.isJiao, e.value);
		}
		
		/*
		*主动接口，通知前台逻辑用户选主牌，有1个参数：e.color表示选择的主牌,1为方块,2为红桃,3为草花，4为红桃
		*/
		private function onSelectColor(e:SelectColorEvent):void
		{
			if(e)
			{
				Out.getInstance().onClaimMianCard(e.color);
				//_sandahaPanel.onSelectColorOver();
			}
		}
		
		
		private function init()
		{
			_tfStatInfo = getChildByName("_msg") as TextField;
			_login=getChildByName("mcLogin") as LogIn;
			_sandahaPanel=getChildByName("mcSanDaHaPanel") as SanDaHaPanel;
			_sandahaPanel.visible = false;
		}
		
		public function onLoginGame(e:LoginEvent):void
		{
			//两个参数 userName-string password-string
			if(_login)
			{
				_login.visible = false;
			}
			
			var sfsUser:SFSUser = new SFSUser(0, e.userName, true);
			var user:User = new User(sfsUser);
			user.setCredit(100);
			user.setExperience(1000);
			user.setGender(0);
			user.setLevel(10);
			user.setSeatNo(0);
			user.setVigour(78);
			
			_nowState=DFState.READY_NO;
			_sandahaPanel.visible = true;
			_sandahaPanel.nowState=_nowState;
			_sandahaPanel.createRoleInfo(user);
			
			Out.getInstance().onLoginGame(e.userName, e.passWord);
		}
	}
}